/* 添加地点 */
var PlaceDetail = require("./detail.js");
var NewMap = require("./newMap.js")
var Tool = require("../common/tool.js");
var NewPlace = React.createClass({
    getInitialState:function(){
        return {
            mapAddress:{}
        }
    },
    componentDidMount:function(){
        var self = this;
        var center = {longitude:120.157696, latitude:30.285087, distance:10000};

        Tool.get('/place/nearby/info').then(function(data){
            var _info = data.locations.list;
            self.refs.map.initMap(_info);
        });
    },
    select:function(e){
        this.setState({
            mapAddress:e
        })
    },
    upload:function(data){
        var self = this;
        var _formData = new FormData();
        Object.keys(data).map(function(item){
            if(!!data[item]){
                _formData.append(item, data[item]);
            }
        });
        Object.keys(self.state.mapAddress).map(function(item){
            var _addr = self.state.mapAddress;
            if(!!_addr[item]){
                _formData.append(item, _addr[item]);
            }
        });

        // 添加地点的接口
        Tool.postForm('/place', _formData).then(function(data){
            alert("添加地点成功");
            // 清空数据
            self.refs.detail.clean();
            self.refs.map.clear();
        });
    },
    render:function(){
        return (
            <div>
                <PlaceDetail upload={this.upload} ref='detail'/>
                <p>{this.state.mapAddress.address}</p>
                <NewMap select={this.select} ref='map'/>
            </div>
        )
    }
});
module.exports = NewPlace;